<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Manage;
use Illuminate\Http\Request;
use function App\Helpers\successMsg;

class ManageController extends Controller
{
    //用户列表
    public function index(Request $request)
    {
        $page = $this->executePage($request);
        $data = Manage::query()->limit($page['per_page'])->offset($page['offset'])->get();
        $total = Manage::query()->count("id");
        $data = [
            'page' => $page['page'],
            'per_page' => $page['per_page'],
            'total' => $total,
            'data' => $data,
        ];
        return successMsg($data);
    }
    //用户添加
    public function insert(Request $request)
    {
        $data = Manage::executeInsertUpdate($request);
        $data['created_at'] = date("Y-m-d H:i:s",time());
        Manage::query()->insert($data);
        return successMsg();
    }
    //用户更新
    public function update(Request $request)
    {
        $id = $request->id ?? 0;
        $data = Manage::executeInsertUpdate($request);
        Manage::query()->where('id',$id)->update($data);
        return successMsg();
    }
    //用户删除
    public function delete(Request $request)
    {
        $ids = $request->ids ?? null;
        $arr_id = is_array($ids)?$ids:explode(',',$ids);
        Manage::query()->whereIn('id',$arr_id)->delete();
        return successMsg();
    }

}
